package socketEnv;

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.net.Socket;
import javax.swing.*;

import com.supermap.desktop.core.Application;
import jUnitPlugin.RunRequiredTest;
import util.Info;

import static java.lang.System.exit;

public class Client {
	public static Socket connection;
	public static ObjectOutputStream output;
	public static ObjectInputStream input;
	public static PrintWriter pw = null;
	public static ObjectOutputStream os = null;
	public static MessageCross message = null;

	public static void connetServer() throws InterruptedException {
		while (true) {
			try {
				// 客户端socket指定服务器的地址和端口号
				connection = new Socket("127.0.0.1", 12310);
			} catch (Exception e) {
				if (!(e instanceof java.net.ConnectException)) {
					e.printStackTrace();
				}
			}
			Thread.sleep(2000);
			System.out.println("try to Connect Server");
			if (connection != null) {
				System.out.println("Client connected :" + connection);
				return;
			}
		}
	}

	public static void send(String s) {
		System.out.println("client to send:" + s);
		if (connection != null && connection.isConnected()) {
			try {
				pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(
						connection.getOutputStream())), true);
				pw.println(s);
				pw.flush();
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}

	public static void send(Object obj) {
		System.out.println("client to send:" + obj.toString());
		if (connection != null && connection.isConnected()
				&& obj instanceof Serializable) {
			try {
				os = new ObjectOutputStream(connection.getOutputStream());
				os.writeObject(obj);
				os.flush();
			} catch (Exception e) {
				// TODO: handle exception
				try{
					//判断服务端已关闭
					connection.sendUrgentData(0xFF);
					connection.sendUrgentData(0xFF);
				}catch(Exception ex){
					Info.out(ex.getMessage());
					Info.out("Server may have shut down.I will exit too..");
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					exit(0);
				}
				e.printStackTrace();
			}
		}
	}

	public static void receive(){
		if (connection != null && connection.isConnected()) {
			try {
				input = new ObjectInputStream(new BufferedInputStream(
						connection.getInputStream()));
				while (true) {
					Object obj = input.readObject();
					System.out.println("client received object:" + obj);
					message = (MessageCross) obj;
					new RunRequiredTest().start();
				}
			} catch (Exception e) {
				// TODO: handle exception
				try{
					//判断服务端已关闭
					connection.sendUrgentData(0xFF);
					connection.sendUrgentData(0xFF);
				}catch(Exception ex){
					Info.out(ex.getMessage());
					Info.out("Server may have shut down.I will exit too..");
					try {
						Thread.sleep(5000);
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					exit(0);
				}
				e.printStackTrace();
			}
		} else {
			System.out.println("Connection Server  failed!");
			return;
		}
	}

	public static void execCommond(String s) {
		System.out.println("execute " + s);
		JButton button = (JButton) Application.getActiveApplication()
				.getMainFrame().getToolbarManager().get(0).getAt(20);
		// send(button);
		send("commond " + s + " executed!");
	}
}
